<?php
namespace app\common\model;
class MemberShare extends Common {
	protected $insert = ['addtime' => SYS_TIME, 'sharedate'];
	protected function setSharedateAttr($value, $data) {
		return date('Y-m-d', SYS_TIME);
	}
	protected function getThumbAttr($value) {
		if (strpos($value, "http://") !== false) {
			return $value;
		} else {
			return $value ? rtrim(APP_URL, "/") . '/' . $value : '';
		}
	}
	//typeid 1:登录，2:介绍建档，3：成交，4：分享海报，5：分享文章，6：分享活动
	public function save_datas($data) {
		// $count = $this->where('userid', $data['userid'])->where('contentid', $data['contentid'])->where('typeid', $data['typeid'])->count();
		// if ($count) {
		// 	return true;
		// }
		switch ($data['typeid']) {
		case 4:
			model('Poster')->where('id', $data['contentid'])->setInc('shares');
			$info = model('Poster')->get_data(['item_id' => $data['contentid']]);
			$linkurl = isset($data['linkurl']) ? $data['linkurl'] : $info['url'];
			$thumb = $info['thumb'];
			break;
		case 5:
			model('Content')->where('id', $data['contentid'])->setInc('shares');
			$info = model('Content')->get_data(['item_id' => $data['contentid']]);
			$linkurl = APP_URL . '/index/news/content/id/' . $info['id'];
			$thumb = $info['image'];
			break;
		case 6:
			break;
		}
		$datas = [
			'userid' => $data['userid'],
			'contentid' => $data['contentid'],
			'title' => $info['title'],
			'content' => isset($data['content']) ? $data['content'] : $info['content'],
			'linkurl' => $linkurl,
			'thumb' => $thumb,
			'status' => 1,
			'typeid' => $data['typeid'],
			'share_type' => $data['share_type'],
		];
		$ids = $this->save_data($datas);
		if ($data['share_type'] == 3) {
			model('Jifen')->save_datas($data['userid'], $data['typeid'], $data['contentid']);
		}
		return true;
	}
	public function list_datas($userid, $typeid) {
		$list = $this->where('userid', $userid)->where('typeid', $typeid)->order('sharedate desc')->select();
		$lists = [];
		$today = date('Y-m-d', SYS_TIME);
		$yesterday = date('Y-m-d', SYS_TIME - 24 * 3600);
		foreach ($list as $v) {
			$v['thumbnail'] = APP_URL . thumb($v['thumb'], 800, 800, 0);
			if (isset($lists[$v['sharedate']])) {
				$lists[$v['sharedate']]['list'][] = $v;
			} else {
				$lists[$v['sharedate']]['title'] = $v['sharedate'] == $today ? '今日分享' : ($v['sharedate'] == $yesterday ? '昨日分享' : $v['sharedate']);
				$lists[$v['sharedate']]['list'][] = $v;
			}
		}
		trace($lists);
		return $lists;
	}
}